<template>
  <div class="business-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>业务列表</span>
          <div>
            <el-button type="primary" @click="handleCreate">新增业务</el-button>
            <el-button @click="handleExport">导出Excel</el-button>
            <el-upload
              action=""
              :show-file-list="false"
              :before-upload="beforeImport"
              accept=".xlsx,.xls"
            >
              <el-button type="success">导入Excel</el-button>
            </el-upload>
          </div>
        </div>
      </template>

      <el-table
        :data="businessList"
        v-loading="loading"
        border
        style="width: 100%"
      >
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="customer.name" label="客户名称" />
        <el-table-column prop="type" label="业务类型" />
        <el-table-column prop="content" label="业务内容" />
        <el-table-column prop="createTime" label="创建时间" width="180">
          <template #default="{ row }">
            {{ formatDate(row.createTime) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template #default="{ row }">
            <el-button size="small" @click="handleEdit(row.id)">编辑</el-button>
            <el-button size="small" type="danger" @click="handleDelete(row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        v-model:current-page="pagination.current"
        v-model:page-size="pagination.size"
        :total="pagination.total"
        layout="total, prev, pager, next, jumper"
        @current-change="fetchData"
      />
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { 
  getBusinessList, 
  deleteBusiness, 
  exportBusiness, 
  importBusiness 
} from '@/api/business'
import { formatDate } from '@/utils/date'

const router = useRouter()

const businessList = ref([])
const loading = ref(false)
const pagination = ref({
  current: 1,
  size: 10,
  total: 0
})

const fetchData = async () => {
  try {
    loading.value = true
    const params = {
      page: pagination.value.current,
      size: pagination.value.size
    }
    const res = await getBusinessList(params)
    businessList.value = res.data
    pagination.value.total = res.total
  } catch (error) {
    ElMessage.error('获取业务列表失败: ' + error.message)
  } finally {
    loading.value = false
  }
}

onMounted(() => {
  fetchData()
})

const handleCreate = () => {
  router.push({ name: 'BusinessCreate' })
}

const handleEdit = (id) => {
  router.push({ name: 'BusinessDetail', params: { id } })
}

const handleDelete = async (id) => {
  try {
    await ElMessageBox.confirm('确定删除该业务吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    await deleteBusiness(id)
    ElMessage.success('删除成功')
    fetchData()
  } catch (error) {
    console.log('取消删除', error)
  }
}

const handleExport = async () => {
  try {
    const res = await exportBusiness()
    const url = window.URL.createObjectURL(new Blob([res]))
    const link = document.createElement('a')
    link.href = url
    link.setAttribute('download', '业务数据.xlsx')
    document.body.appendChild(link)
    link.click()
    document.body.removeChild(link)
  } catch (error) {
    ElMessage.error('导出失败: ' + error.message)
  }
}

const beforeImport = async (file) => {
  try {
    await importBusiness(file)
    ElMessage.success('导入成功')
    fetchData()
    return false
  } catch (error) {
    ElMessage.error('导入失败: ' + error.message)
    return false
  }
}
</script>

<style scoped>
.business-container {
  padding: 20px;
}
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>